/**
 ****************************************************************************************
 *
 * @file user_periph_setup.c
 *
 * @brief Peripherals setup and initialization. 
 *
 * Copyright (C) 2012. Dialog Semiconductor Ltd, unpublished work. This computer 
 * program includes Confidential, Proprietary Information and is a Trade Secret of 
 * Dialog Semiconductor Ltd.  All use, disclosure, and/or reproduction is prohibited 
 * unless authorized in writing. All Rights Reserved.
 *
 * <bluetooth.support@diasemi.com> and contributors.
 *
 ****************************************************************************************
 */
/*
 * INCLUDE FILES
 ****************************************************************************************
 */
#include "rwip_config.h"             // SW configuration
#include "user_periph_setup.h"            // periphera configuration
#include "global_io.h"
#include "gpio.h"
#include "uart.h"                    // UART initialization

#include "user_proxr.h"

/**
 ****************************************************************************************
 * @brief Each application reserves its own GPIOs here.
 *
 * @return void
 ****************************************************************************************
 */

#if DEVELOPMENT_DEBUG

void GPIO_reservations(void)
{

/*
* Application specific GPIOs reservation
*/    
#if 1//BLE_PROX_REPORTER
    #if USE_PUSH_BUTTON
    RESERVE_GPIO( PUSH_BUTTON, GPIO_BUTTON_PORT, GPIO_BUTTON_PIN, PID_GPIO);   
    #endif // USE_PUSH_BUTTON
	
    RESERVE_GPIO( MOTOR_CTL, GPIO_ALERT_MOTOR_PORT, GPIO_ALERT_MOTOR_PIN, PID_GPIO);
		RESERVE_GPIO( DISMANTLE_CHECK1, GPIO_DISMANTLE_CHECK1_PORT, GPIO_DISMANTLE_CHECK1_PIN, PID_GPIO);
		RESERVE_GPIO( DISMANTLE_CHECK2, GPIO_DISMANTLE_CHECK2_PORT, GPIO_DISMANTLE_CHECK2_PIN, PID_GPIO);

		RESERVE_GPIO( PWR_HOLD, GPIO_PWR_HOLD_PORT, GPIO_PWR_HOLD_PIN, PID_GPIO);

		RESERVE_GPIO( ADC_BAT, GPIO_ADC_BAT_PORT, GPIO_ADC_BAT_PIN, PID_ADC );

		RESERVE_GPIO( CHG_DET, GPIO_CHG_DET_PORT, GPIO_CHG_DET_PIN, PID_GPIO);  
		 
#endif
#if BLE_BAS_SERVER && USE_BAT_LEVEL_ALERT
	//Setup LED GPIO for battery alert
    RESERVE_GPIO( RED_LED, GPIO_BAT_LED_PORT, GPIO_BAT_LED_PIN, PID_GPIO);
#endif
    
#if (BLE_SPOTA_RECEIVER)

    #if !defined(__DA14583__)
    RESERVE_GPIO(SPI_EN,  SPI_EN_GPIO_PORT,  SPI_EN_GPIO_PIN,  PID_SPI_EN);
    RESERVE_GPIO(SPI_CLK, SPI_CLK_GPIO_PORT, SPI_CLK_GPIO_PIN, PID_SPI_CLK);
    RESERVE_GPIO(SPI_DO,  SPI_DO_GPIO_PORT,  SPI_DO_GPIO_PIN,  PID_SPI_DO);
    RESERVE_GPIO(SPI_DI,  SPI_DI_GPIO_PORT,  SPI_DI_GPIO_PIN,  PID_SPI_DI);
    #else
        // The DA14583 GPIOs that are dedicated to its internal SPI flash
        // are automaticaly reserved by the GPIO driver.
    #endif
    
    // Example GPIO reservations for an I2C EEPROM.
    //RESERVE_GPIO( I2C_SCL, GPIO_PORT_0, GPIO_PIN_2, PID_I2C_SCL);
    //RESERVE_GPIO( I2C_SDA, GPIO_PORT_0, GPIO_PIN_3, PID_I2C_SDA);
#endif
    
#ifdef CFG_PRINTF_UART2

		RESERVE_GPIO(UART_TX, UART2_GPIO_PORT, UART2_TX_PIN, PID_UART2_TX);
    RESERVE_GPIO(UART_RX, UART2_GPIO_PORT, UART2_RX_PIN, PID_UART2_RX);
	
#endif

#if (USE_I2C)

		RESERVE_GPIO(I2C_CLK,  I2C_PORT, I2C_SCL_PIN, PID_I2C_SCL);
		RESERVE_GPIO(I2C_SDA,  I2C_PORT, I2C_SDA_PIN, PID_I2C_SDA);
	
#endif 

#if USE_OLED_SH1107

		RESERVE_GPIO(OLED_RES,  OLED_RES_PORT, OLED_RES_PIN, PID_GPIO);
		RESERVE_GPIO(OLED_VPP_EN,  OLED_VPP_EN_PORT, OLED_VPP_EN_PIN, PID_GPIO);

#endif
}
#endif

/**
 ****************************************************************************************
 * @brief Map port pins
 *
 * The Uart and SPI port pins and GPIO ports(for debugging) are mapped
 ****************************************************************************************
 */
void set_pad_functions(void)        // set gpio port function mode
{
    
#if 1 //BLE_PROX_REPORTER
    #if USE_PUSH_BUTTON
    GPIO_ConfigurePin( GPIO_BUTTON_PORT, GPIO_BUTTON_PIN, INPUT_PULLDOWN, PID_GPIO, false ); // Push Button 
    #endif // USE_PUSH_BUTTON

		if (alert_motor_get_status()) {
    	GPIO_ConfigurePin( GPIO_ALERT_MOTOR_PORT, GPIO_ALERT_MOTOR_PIN, OUTPUT, PID_GPIO, true ); //Alert MOTOR
		} else {
			GPIO_ConfigurePin( GPIO_ALERT_MOTOR_PORT, GPIO_ALERT_MOTOR_PIN, OUTPUT, PID_GPIO, false ); //Alert MOTOR
		}
		
		GPIO_ConfigurePin( GPIO_DISMANTLE_CHECK1_PORT, GPIO_DISMANTLE_CHECK1_PIN, INPUT_PULLUP, PID_GPIO, false );
		GPIO_ConfigurePin( GPIO_DISMANTLE_CHECK2_PORT, GPIO_DISMANTLE_CHECK2_PIN, INPUT_PULLUP, PID_GPIO, false );

		GPIO_ConfigurePin( GPIO_PWR_HOLD_PORT, GPIO_PWR_HOLD_PIN, OUTPUT, PID_GPIO, true );

		GPIO_ConfigurePin( GPIO_ADC_BAT_PORT, GPIO_ADC_BAT_PIN, INPUT, PID_ADC, false );

		GPIO_ConfigurePin( GPIO_CHG_DET_PORT, GPIO_CHG_DET_PIN, INPUT, PID_GPIO, false ); // charge detect pin 

#endif
#if BLE_BAS_SERVER  && USE_BAT_LEVEL_ALERT
    GPIO_ConfigurePin( GPIO_BAT_LED_PORT, GPIO_BAT_LED_PIN, OUTPUT, PID_GPIO, false ); //Battery alert LED
#endif
    
#if (BLE_SPOTA_RECEIVER)
    GPIO_ConfigurePin( SPI_EN_GPIO_PORT,  SPI_EN_GPIO_PIN,  OUTPUT, PID_SPI_EN, true );
    GPIO_ConfigurePin( SPI_CLK_GPIO_PORT, SPI_CLK_GPIO_PIN, OUTPUT, PID_SPI_CLK, false );
    GPIO_ConfigurePin( SPI_DO_GPIO_PORT,  SPI_DO_GPIO_PIN,  OUTPUT, PID_SPI_DO, false );
    GPIO_ConfigurePin( SPI_DI_GPIO_PORT,  SPI_DI_GPIO_PIN,  INPUT,  PID_SPI_DI, false );

    // Example GPIO configuration for an I2C EEPROM.
    //GPIO_ConfigurePin(GPIO_PORT_0, GPIO_PIN_2, INPUT, PID_I2C_SCL, false);
    //GPIO_ConfigurePin(GPIO_PORT_0, GPIO_PIN_3, INPUT, PID_I2C_SDA, false);
#endif

#ifdef CFG_PRINTF_UART2

		GPIO_ConfigurePin(UART2_GPIO_PORT, UART2_TX_PIN, OUTPUT, PID_UART2_TX, false);
    GPIO_ConfigurePin(UART2_GPIO_PORT, UART2_RX_PIN, INPUT, PID_UART2_RX, false);
		SetBits16(CLK_PER_REG, UART2_ENABLE, 1);      // enable  clock for UART 2
    uart2_init(UART2_BAUDRATE, UART2_DATALENGTH);	

#endif

#if (USE_I2C)
	GPIO_ConfigurePin(I2C_PORT, I2C_SCL_PIN, OUTPUT, PID_I2C_SCL, false);
	GPIO_ConfigurePin(I2C_PORT, I2C_SDA_PIN, OUTPUT, PID_I2C_SDA, false);
#endif

#if USE_OLED_SH1107

	GPIO_ConfigurePin( OLED_RES_PORT, OLED_RES_PIN, OUTPUT, PID_GPIO, false );
	GPIO_ConfigurePin( OLED_VPP_EN_PORT, OLED_VPP_EN_PIN, OUTPUT, PID_GPIO, false );

#endif



}

bool  alert_motor_status __attribute__((section("retention_mem_area0"),zero_init)); //@RETENTION MEMORY

void alert_motor_set_on(bool on)
{
		alert_motor_status = on;
	 	if(on) {
        GPIO_SetActive( GPIO_ALERT_MOTOR_PORT, GPIO_ALERT_MOTOR_PIN);
    } else{
        GPIO_SetInactive( GPIO_ALERT_MOTOR_PORT, GPIO_ALERT_MOTOR_PIN);
    }
}

bool alert_motor_get_status(void)
{
		return alert_motor_status;//GPIO_GetPinStatus( GPIO_ALERT_MOTOR_PORT, GPIO_ALERT_MOTOR_PIN );
}

void power_hold_set_on(bool on)
{

	//GPIO_SetInactive( GPIO_PWR_HOLD_PORT, GPIO_PWR_HOLD_PIN);
	 #if 1
		//GPIO_ConfigurePin( GPIO_PWR_HOLD_PORT, GPIO_PWR_HOLD_PIN, OUTPUT, PID_GPIO, true);
	 	if(on) {
        GPIO_SetActive( GPIO_PWR_HOLD_PORT, GPIO_PWR_HOLD_PIN);
    } else{
        GPIO_SetInactive( GPIO_PWR_HOLD_PORT, GPIO_PWR_HOLD_PIN);
    }
	#endif
}


bool app_is_power_hold_on(void)
{
		return true;//GPIO_GetPinStatus( GPIO_PWR_HOLD_PORT, GPIO_PWR_HOLD_PIN );
}

bool app_is_battery_charging(void)
{
		return GPIO_GetPinStatus( GPIO_CHG_DET_PORT, GPIO_CHG_DET_PIN );
}


/**
 ****************************************************************************************
 * @brief Enable pad's and peripheral clocks assuming that peripherals' power domain is down. The Uart and SPi clocks are set.
 *
 * @return void
 ****************************************************************************************
 */
void periph_init(void)  // set i2c, spi, uart, uart2 serial clks
{
	// Power up peripherals' power domain
    SetBits16(PMU_CTRL_REG, PERIPH_SLEEP, 0);
    while (!(GetWord16(SYS_STAT_REG) & PER_IS_UP)) ; 
    
    SetBits16(CLK_16M_REG, XTAL16_BIAS_SH_ENABLE, 1);
	
	//rom patch
	patch_func();
	
	//Init pads
	set_pad_functions();


	//app_dismantle_check_enable();

#if 1// BLE_PROX_REPORTER
   // app_proxr_port_reinit(GPIO_ALERT_LED_PORT, GPIO_ALERT_LED_PIN);
    #if USE_PUSH_BUTTON
    app_button_enable();
    #endif // USE_PUSH_BUTTON
		
#elif BLE_FINDME_LOCATOR
    #if USE_PUSH_BUTTON
    app_button_enable();
    #endif // USE_PUSH_BUTTON
#endif //BLE_PROX_REPORTER
#if 0//BLE_BATTERY_SERVER
    app_batt_port_reinit();
#endif //BLE_BATTERY_SERVER


    // Enable the pads
	SetBits16(SYS_CTRL_REG, PAD_LATCH_EN, 1);
}

